package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.generator.entity.WebRotationEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import net.bytebuddy.asm.Advice;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.WebAdministratorEntity;
import io.renren.modules.generator.service.WebAdministratorService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 管理者表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-24 12:56:47
 */
@RestController
@RequestMapping("generator/webadministrator")
@Api(value = "管理者",tags = {"管理者"})
public class WebAdministratorController {
    @Autowired
    private WebAdministratorService webAdministratorService;

    @Autowired
    private SysUserService sysUserService;
    /**
     * 查询最大排序码
     */
    @RequestMapping("/getMaxSort")
    @RequiresPermissions("generator:webrotation:list")
    public Integer getMaxSort(){
        Integer maxSort = webAdministratorService.getMaxSort();
        return maxSort==null?0:maxSort;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:webadministrator:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = webAdministratorService.queryPage(params);
        List<WebAdministratorEntity> list = (List<WebAdministratorEntity>) page.getList();
        for (WebAdministratorEntity webAdministratorEntity : list) {
            Integer createUser = webAdministratorEntity.getCreateUser();
            SysUserEntity sysUserServiceById = sysUserService.getById(createUser);
            webAdministratorEntity.setUserName(sysUserServiceById.getUsername());
        }
        page.setList(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{adminId}")
    @RequiresPermissions("generator:webadministrator:info")
    public R info(@PathVariable("adminId") Integer adminId){
		WebAdministratorEntity webAdministrator = webAdministratorService.getById(adminId);

        return R.ok().put("webAdministrator", webAdministrator);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:webadministrator:save")
    public R save(@RequestBody WebAdministratorEntity webAdministrator){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Long userId = userEntity.getUserId();
        webAdministrator.setCreateUser(Math.toIntExact(userId));
        webAdministrator.setCreateTime(new Date());
        webAdministrator.setUpdateTime(new Date());
		webAdministratorService.save(webAdministrator);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:webadministrator:update")
    public R update(@RequestBody WebAdministratorEntity webAdministrator){
		webAdministratorService.updateById(webAdministrator);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:webadministrator:delete")
    public R delete(@RequestBody Integer[] adminIds){
		webAdministratorService.removeByIds(Arrays.asList(adminIds));

        return R.ok();
    }

}
